package org.LZQ.ShortLink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.LZQ.ShortLink.project.common.convention.result.Result;
import org.LZQ.ShortLink.project.common.convention.result.Results;
import org.LZQ.ShortLink.project.dto.req.ShortLinkBatchCreateReqDTO;
import org.LZQ.ShortLink.project.dto.req.ShortLinkCreateReqDTO;
import org.LZQ.ShortLink.project.dto.req.ShortLinkPageReqDTO;
import org.LZQ.ShortLink.project.dto.req.ShortLinkUpdateReqDTO;
import org.LZQ.ShortLink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import org.LZQ.ShortLink.project.dto.resp.ShortLinkCreateRespDTO;
import org.LZQ.ShortLink.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import org.LZQ.ShortLink.project.dto.resp.ShortLinkPageRespDTO;
import org.LZQ.ShortLink.project.handler.CustomBlockHandler;
import org.LZQ.ShortLink.project.service.ShortLinkService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
public class ShortLinkController {
    @Resource
    private  ShortLinkService shortLinkService;
    /**
     * 短链接跳转原始链接
     */
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) throws IOException {
        shortLinkService.restoreUrl(shortUri, request, response);
    }
    /**
     * 创建短链接
     */
    @PostMapping("/api/shortLink/project/shortLink/create")
    @SentinelResource(value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam) {
        return Results.success(shortLinkService.createShortLink(requestParam));
    }

    /**
     * 通过分布式锁创建短链接
     */
    @PostMapping("/api/shortLink/admin/shortLink/create/by-lock")
    public Result<ShortLinkCreateRespDTO> createShortLinkByLock(@RequestBody ShortLinkCreateReqDTO requestParam) {
        return Results.success(shortLinkService.createShortLinkByLock(requestParam));
    }
    /**
     * 批量创建短链接
     */
    @PostMapping("/api/shortLink/project/shortLink/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return Results.success(shortLinkService.batchCreateShortLink(requestParam));
    }

    /**
     * 分页查询短链接
     * @param requestParam
     * @return
     */
    @GetMapping("/api/shortLink/project/shortLink/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        return Results.success(shortLinkService.pageShortLink(requestParam));
    }
    /**
     * 查询短链接分组内数量
     */
    @GetMapping("/api/shortLink/project/shortLink/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam) {
        return Results.success(shortLinkService.listGroupShortLinkCount(requestParam));
    }

    /**
     * 短连接信息更新
     * @param requestParam
     * @return
     */
    @PostMapping("/api/shortLink/project/shortLink/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }

}
